#include<cstdio>
#include<cstring>
const int N=110;
int w[N];
double f[10010];
double need,v[N];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		memset(f,0,sizeof(f));
		int n,m=0;
		scanf("%lf%d",&need,&n);
		need=1-need;
		for(int i=1;i<=n;i++)
		{
			scanf("%d%lf",&w[i],&v[i]);
			v[i]=1-v[i];
			m+=w[i];
		}
		f[0]=1;
		for(int i=1;i<=n;i++)
			for(int j=m;j>=0;j--)
				if(j>=w[i]&&f[j-w[i]]*v[i]>f[j])
					f[j]=f[j-w[i]]*v[i];
		int ans=0;
		for(int i=m;i>=0;i--)
			if(f[i]>=need){ans=i;break;}
		printf("%d\n",ans);
	}
	return 0;
}
